<?php

namespace App\Http\Controllers\Frontend\V10\Shop;

use App\Http\Controllers\Controller;
use App\Http\Requests\Frontend\V10\Shop\GetTokenByUserIdRequest;
use App\Library\Guzzle\BbpPlatformRequest;

class TokenController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    /**
     * Description:获取用户token
     * Author: Sam
     * DateTime: 2019/1/15 17:57
     * @param GetTokenByUserIdRequest $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getTokenByUserId(GetTokenByUserIdRequest $request)
    {
        $user = $request->user();
        $user_id = md5($user->id.$user->created_at);
        $res = BbpPlatformRequest::getTokenByUserId($user_id);
        if($res['result']==true){
            $data = [
                'index_url'=>config('common.shop_index_url').$res['object']['token'],
                'order_url'=>config('common.shop_order_url').$res['object']['token'],
            ];
            return success($data);
        }else{
            $res = BbpPlatformRequest::syncUser($user->phone_num,$user_id);
            $data = [

                'index_url'=>config('common.shop_index_url').$res['object']['token'],
                'order_url'=>config('common.shop_order_url').$res['object']['token'],
            ];
            return success($data);
        }
    }
}
